﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.ReportArea
{
	public partial class ReportOfAreaFirstLevel : BasePage
	{
		DataTable dt = new DataTable();
		protected string HospitalTotal, DrugsTotal, FactoryTotal, DistributionTotal, OrderAmountTotal;
		protected string HospitalTotal_PS, DrugsTotal_PS, FactoryTotal_PS, DistributionTotal_PS, DistributionAmountTotal, StorageAmountTotal;
		HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();

		public ReportOfAreaFirstLevel()
		{
			this.ModuleNo = Module.地区统计.ToString();
			this.PageOperateType = OperateType.地区统计查看.ToString();
		}

		protected void Page_Load(object sender, EventArgs e)
		{

			if (!IsPostBack)
			{

				string areid = Request.QueryString["aid"];

				if (!string.IsNullOrEmpty(areid))
				{
					string sessionid = Guid.NewGuid().ToString();
					SuperviseQuery query = new SuperviseQuery();
					try
					{
						query.AreaID = areid;
						query.AreaName = HZ.Supervise.Web.Function.ReturnAreaName(query.AreaID);

					}
					catch { }

					Session[sessionid] = query;
					ViewState["query"] = query;
				}
				else
				{
					string sessionid = Guid.NewGuid().ToString();
					SuperviseQuery query = new SuperviseQuery();
					if (CurrentAccount.GetUser().Org.Area.AreaID != Constants.CURRENT_DEPLOY_AREAID)
					{
						query.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;
						query.AreaName = HZ.Supervise.Web.Function.ReturnAreaName(query.AreaID);
						Session[sessionid] = query;
						ViewState["query"] = query;

					}
				}
				//bindTB();
				Databind();
				BindChart();
				GetStatistics();
			}
		}
		void GetStatistics()
		{
			double purchaseAmountTotal = 0;//采购总金额
			double PurchaseCountTotal = 0;
			double wareHouseAmountTotal = 0;//入库总金额
			double wareHouseCountTotal = 0;
			double distributeAmountTotal = 0;//配送总金额

			double distributeCountTotal = 0;//配送总数量
            double SellAmountTotal = 0;//销售总金额
            double ReturnAmountTotal = 0;//退货总金额
			if (dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					if (dt.Rows[i]["PurchaseAmountTotal"] != DBNull.Value) { purchaseAmountTotal += double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()); }
				
					if (dt.Rows[i]["WareHouseAmountTotal"] != DBNull.Value) { wareHouseAmountTotal += double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()); }
				
					if (dt.Rows[i]["DistributeAmountTotal"] != DBNull.Value) { distributeAmountTotal += double.Parse(dt.Rows[i]["DistributeAmountTotal"].ToString()); }
			
                    if (dt.Rows[i]["SellAmountTotal"] != DBNull.Value) { SellAmountTotal += double.Parse(dt.Rows[i]["SellAmountTotal"].ToString()); }
                    if (dt.Rows[i]["ReturnAmountTotal"] != DBNull.Value) { ReturnAmountTotal += double.Parse(dt.Rows[i]["ReturnAmountTotal"].ToString()); }

				}
                this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributeAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString() + ";销售总金额为:" + SellAmountTotal.ToString() + ";退货总金额为:" + ReturnAmountTotal.ToString();

                //this.lblOrderAmountTotal.Text = purchaseAmountTotal.ToString();

                //this.lblOrderCountTotal.Text = PurchaseCountTotal.ToString();

                //this.lblWareHouseAmountTotal.Text = wareHouseAmountTotal.ToString();

                //this.lblWareHouseCountTotal.Text = wareHouseCountTotal.ToString();

                //this.lblDistributionAmountTotal.Text = distributeAmountTotal.ToString();

                //this.lblDistributionCountTotal.Text = distributeCountTotal.ToString();

			}

		}
		/// <summary>
		/// 绑定table
		/// by张雄  2011.5.6
		/// </summary>
		private void bindTB()
		{
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

			var model = new HZ.Data.QueryModel();
			if (sq != null)
			{
				if (sq.AreaID != null && sq.AreaID != "")
				{
					model["AreaId"] = sq.AreaID;
				}
			}
			else
			{
                if (CurrentAccount.GetUser().Org.Area.AreaID != "510000")
				{
					model["AreaId"] = CurrentAccount.GetUser().Org.Area.AreaID;
				}

			}

			DataTable dtChart1 = ie.GetSuperviseChart1(model);

            //if (dtChart1.Rows.Count > 0)
            //{
            //    if (dtChart1.Rows[0]["HospitalCount"] != DBNull.Value) { this.lblHospitalTotal.Text = string.Format("{0}个", dtChart1.Rows[0]["HospitalCount"].ToString()); }
            //    if (dtChart1.Rows[0]["ProcureCatalogCount"] != DBNull.Value) { lblDrugsTotal.Text = string.Format("{0}个", dtChart1.Rows[0]["ProcureCatalogCount"].ToString()); }
            //    if (dtChart1.Rows[0]["CompanySCCount"] != DBNull.Value) { lblFactoryTotal.Text = string.Format("{0}个", dtChart1.Rows[0]["CompanySCCount"].ToString()); }
            //    if (dtChart1.Rows[0]["CompanyPSCount"] != DBNull.Value) { lblDistributionTotal.Text = string.Format("{0}个", dtChart1.Rows[0]["CompanyPSCount"].ToString()); }

            //}

		}

		public void Databind()
		{
			SuperviseQuery sq;
			if (ViewState["query"] != null)
			{
				sq = (SuperviseQuery)ViewState["query"];
			}
			else
			{
				sq = new SuperviseQuery();
				HZ.Supervise.Model.User user = CurrentAccount.GetUser();
				if (user.Org.Area.AreaID.Equals(Constants.CURRENT_DEPLOY_AREAID.ToString()))
				{ }
				else
				{
					sq.AreaID = user.Org.Area.AreaID;
				}
			}

			var model = new HZ.Data.QueryModel();
			model["AreaId"] = sq.AreaID;
			model["SortFields"] = "DayTime";
			string month = "";
			if (DateTime.Now.AddMonths(-12).Month <= 9) month = "0" + DateTime.Now.AddMonths(-12).Month.ToString();
			else month = DateTime.Now.AddMonths(-12).Month.ToString();
			string timeFrom = DateTime.Now.AddMonths(-12).Year + "" + month;
			if (string.IsNullOrEmpty(sq.Append))
			{
				model["append"] = " DayTime > '" + timeFrom + "'";
			}
			else
			{
				model["append"] = " DayTime > '" + timeFrom + "' and " + sq.Append;
			}

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
			System.Data.DataTable dt2 = iNew.DayTime_SortSorec_AreaId_GroupBy(model, 1, 0, out t);
			dt = dt2.Copy();

			DataView dv = dt2.DefaultView;

			dv.Sort = "DayTime desc";

			dt2 = dv.ToTable();

            //HZ.Supervise.Web.Function.AddColumn(dt2, "WareHouseRate", "SortSorce");
            //HZ.Supervise.Web.Function.ChangeRate_PS(dt2, "DistributeRate");

			gvwProcureCatalog.DataSource = dt2;

			gvwProcureCatalog.DataBind();

            //HZ.Supervise.Web.Function.Combine(gvwProcureCatalog, 0, 0);
            //HZ.Supervise.Web.Function.Combine(gvwProcureCatalog, 0, 7);

		}

		/// <summary>
		/// 图表绑定 
		/// </summary>
		private void BindChart()
		{

			//this.tbChange.Visible = true;

			this.btnExcel.Visible = true;

			this.divshow1.Visible = true;

			//lblnull.Visible = false;

			this.ChartInStorage.Visible = true;

            this.ChartPurchasing.Visible = true;

			this.divshow1.Visible = true;

			//modify  by 曹鹏 2011年5月5日16:00:06 


			//月份变量
			string monch = "";

            ////日常采购提示循环因子
            //int BaseI = 0;

            ////临时采购提示循环因子
            //int ComplementI = 0;

            ////限制品种提示循环因子
            //int AstrictI = 0;

			if (dt.Rows.Count > 0)
			{
				#region

                //#region 数量序列集

                ////日常采购
                //Series BaseCatalog = new Series("日常采购");
				
                //    BaseCatalog.ChartType = SeriesChartType.Column;
				
                ////设置图表边宽
                //BaseCatalog.BorderWidth = 1;
                //BaseCatalog.MarkerStyle = MarkerStyle.Diamond;
                ////设置图表偏移量
                //BaseCatalog.ShadowOffset = 1;

                ////BaseCatalog.Color = Color.DarkGray;

                //BaseCatalog.Color = Color.FromArgb(74, 172, 197);
                //BaseCatalog.Label = "#VAL{F}";

                //BaseCatalog.MarkerStyle = MarkerStyle.Diamond;


                ///**************************************************************************/

                ////临时采购
                //Series Complementarity = new Series("临时采购");
               
                //    Complementarity.ChartType = SeriesChartType.Column;
                
                ////设置图表边宽
                //Complementarity.BorderWidth = 1;

                //Complementarity.MarkerStyle = MarkerStyle.Diamond;
                ////设置图表偏移量
                //Complementarity.ShadowOffset = 1;

                //Complementarity.Color = Color.FromArgb(129, 101, 162);

                //Complementarity.MarkerStyle = MarkerStyle.Diamond;

                ///**************************************************************************/

                ////应急采购
                //Series Astrictbreed = new Series("应急采购");
               
                //    Astrictbreed.ChartType = SeriesChartType.Column;
              
                ////设置图表边宽
                //Astrictbreed.BorderWidth = 1;
                //Astrictbreed.MarkerStyle = MarkerStyle.Diamond;

                //Astrictbreed.Color = Color.FromArgb(192, 80, 78);
                //Astrictbreed.Label = "#VAL{F}";

                ////设置图表偏移量
                //Astrictbreed.ShadowOffset = 1;

                ////Astrictbreed.Color = Color.Red;

                //Astrictbreed.MarkerStyle = MarkerStyle.Diamond;

                //#endregion

                //#region 金额序列集

                //// 0
                //Series BaseCatalogMoney = new Series("日常采购");

				
                //    BaseCatalogMoney.ChartType = SeriesChartType.Column;
			
                //BaseCatalogMoney.BorderWidth = 1;

                //BaseCatalogMoney.ShadowOffset = 1;

                //BaseCatalogMoney.MarkerStyle = MarkerStyle.Diamond;
                //BaseCatalogMoney.Color = Color.FromArgb(74, 172, 197);
                //BaseCatalogMoney.Label = "#VAL{F}";
                //BaseCatalogMoney.MarkerStyle = MarkerStyle.Diamond;
                //BaseCatalogMoney.ToolTip = "日常采购: #VAL{C} 万元";
                ///**************************************************************************/

                //// 1
                //Series ComplementarityMoney = new Series("临时采购");

               
                //    ComplementarityMoney.ChartType = SeriesChartType.Column;
                

                //ComplementarityMoney.BorderWidth = 1;

                //ComplementarityMoney.MarkerStyle = MarkerStyle.Diamond;

                //ComplementarityMoney.ShadowOffset = 1;

                //ComplementarityMoney.Color = Color.FromArgb(129, 101, 162);
                //ComplementarityMoney.Label = "#VAL{F}";
                //ComplementarityMoney.MarkerStyle = MarkerStyle.Diamond;
                //ComplementarityMoney.ToolTip = "临时采购:#VAL{C} 万元";

                ///**************************************************************************/

                //// 5
                //Series AstrictbreedMoney = new Series("应急采购");
               
                //    AstrictbreedMoney.ChartType = SeriesChartType.Column;
               
                //AstrictbreedMoney.BorderWidth = 1;
                //AstrictbreedMoney.MarkerStyle = MarkerStyle.Diamond;


                //AstrictbreedMoney.ShadowOffset = 1;
                //AstrictbreedMoney.Label = "#VAL{F}";
                //AstrictbreedMoney.Color = Color.FromArgb(192, 80, 78);
                //AstrictbreedMoney.MarkerStyle = MarkerStyle.Diamond;
                //AstrictbreedMoney.ToolTip = "应急采购：#VAL{C} 万元";

                //#endregion

                //#region 逻辑构造图表数据

                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    //如果是第一行,则初始化
                //    if (i == 0)
                //    {
                //        //第一行月份放入变量中,用于第一次判断
                //        monch = dt.Rows[0]["DayTime"].ToString();

                //        //如果为
                //        if (dt.Rows[0]["SortSorce"].ToString().Equals("0"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[0], "0", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //如果为
                //        if (dt.Rows[0]["SortSorce"].ToString().Equals("1"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[0], "1", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //如果为
                //        if (dt.Rows[0]["SortSorce"].ToString().Equals("2"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[0], "2", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //日常采购循环因子自增
                //        BaseI++;

                //        //临时采购循环因子自增
                //        ComplementI++;

                //        //应急采购循环因子自增
                //        AstrictI++;

                //        //跳过下边的语句，重新循环执行
                //        continue;
                //    }

                //    //不为第一行数据
                //    //如果当前行的月份与上一行中的月份相同
                //    if (dt.Rows[i]["DayTime"].ToString().Equals(monch))
                //    {
                //        switch (dt.Rows[i]["SortSorce"].ToString())
                //        {
                //            case "0":

                //                BindChartFinishData(dt.Rows[i], "0", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);


                //                break;

                //            case "1":

                //                BindChartFinishData(dt.Rows[i], "1", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);

                //                break;

                //            case "2":

                //                BindChartFinishData(dt.Rows[i], "2", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);

                //                break;
                //        }

                //        //重新赋值月份
                //        monch = dt.Rows[i]["DayTime"].ToString();
                //    }
                //    else
                //    {
                //        //如果为日常采购
                //        if (dt.Rows[i]["SortSorce"].ToString().Equals("0"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[i], "0", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //如果为临时采购
                //        if (dt.Rows[i]["SortSorce"].ToString().Equals("1"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[i], "1", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //如果为应急采购
                //        if (dt.Rows[i]["SortSorce"].ToString().Equals("2"))
                //        {
                //            //调用方法初始化数据
                //            BindChartInit(dt.Rows[i], "2", BaseI, ComplementI, AstrictI, BaseCatalog, Complementarity, Astrictbreed, BaseCatalogMoney, ComplementarityMoney, AstrictbreedMoney);
                //        }

                //        //重新赋值月份
                //        monch = dt.Rows[i]["DayTime"].ToString();

                //        //日常采购循环因子自增
                //        BaseI++;

                //        //临时采购循环因子自增
                //        ComplementI++;

                //        //应急采购循环因子自增
                //        AstrictI++;
                //    }
                //}

                //#endregion

                //#region 序列集合属性设置

                //#region 加入金额图表

                //BaseCatalogMoney.IsValueShownAsLabel = true;

                //ChartPurchasing.Series.Add(BaseCatalogMoney);

                ////日常采购金额加入图表中
                //ChartPurchasing.Series["日常采购"].ChartArea = "ChartAreaPurchasing";


                //ComplementarityMoney.IsValueShownAsLabel = true;

                //ChartPurchasing.Series.Add(ComplementarityMoney);

                ////临时采购金额加入图表中
                //ChartPurchasing.Series["临时采购"].ChartArea = "ChartAreaPurchasing";


                //AstrictbreedMoney.IsValueShownAsLabel = true;

                //ChartPurchasing.Series.Add(AstrictbreedMoney);

                ////应急采购金额加入图表中 
                //ChartPurchasing.Series["应急采购"].ChartArea = "ChartAreaPurchasing";

                //#endregion

                //#region 加入数量图表

                //BaseCatalog.IsValueShownAsLabel = true;

                //ChartInStorage.Series.Add(BaseCatalog);

                ////日常采购金额加入图表中 
                //ChartInStorage.Series["日常采购"].ChartArea = "ChartAreaStorage";


                //Complementarity.IsValueShownAsLabel = true;

                //ChartInStorage.Series.Add(Complementarity);

                ////临时采购金额加入图表中 
                //ChartInStorage.Series["临时采购"].ChartArea = "ChartAreaStorage";


                //Astrictbreed.IsValueShownAsLabel = true;

                //ChartInStorage.Series.Add(Astrictbreed);
                ////应急采购金额加入图表中
                //ChartInStorage.Series["应急采购"].ChartArea = "ChartAreaStorage";

                //#endregion

                //#region 图表属性设置

                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.IsMarginVisible = true;
                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].Area3DStyle.Enable3D = false;

                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.IsMarginVisible = true;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].Area3DStyle.Enable3D = false;


                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.MajorGrid.Interval = 1;
                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.LineWidth = 1;
                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.LineWidth = 1;

                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.MajorGrid.Interval = 1;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.LineWidth = 1;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisY.LineWidth = 1;

                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.Title = "金额（万元）";
                //ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.Title = "日期";

                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisY.Title = "配送率";
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.Title = "日期";

                //#endregion

                //#region 加入图表

                //ChartPurchasing.Series["日常采购"]["StackedGroupName"] = "Group1";
                //ChartPurchasing.Series["临时采购"]["StackedGroupName"] = "Group1";
                //ChartPurchasing.Series["应急采购"]["StackedGroupName"] = "Group1";

                //ChartInStorage.Series["日常采购"]["StackedGroupName"] = "Group2";
                //ChartInStorage.Series["临时采购"]["StackedGroupName"] = "Group2";
                //ChartInStorage.Series["应急采购"]["StackedGroupName"] = "Group2";

                //ChartPurchasing.ResetAutoValues();

                //ChartInStorage.ResetAutoValues();

                //#endregion

                //#endregion

				#endregion
				//lblnull.Visible = false;

                #region 左图

                //***************** 日常采购 *****************/
                Series series1 = new Series("采购金额");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series1.IsValueShownAsLabel = true;
                series1.MarkerStyle = MarkerStyle.Diamond;
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;
                series1.ToolTip = "采购金额: #VAL{c} 万元";


                series1.Label = "#VAL{F}";
                series1.Color = Color.FromArgb(243, 84, 37);


                //***************** 临时采购 *****************/
                Series series2 = new Series("配送金额");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series2.IsValueShownAsLabel = true;
                series2.MarkerStyle = MarkerStyle.Diamond;
                series2.BorderWidth = 1;

                series2.ShadowOffset = 1;
                series2.ToolTip = "配送金额: #VAL{c}  万元";

                series2.Label = "#VAL{F}";
                series2.Color = Color.FromArgb(243, 152, 0);


                //***************** 应急采购 *****************/
                Series series3 = new Series("入库金额");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series3.IsValueShownAsLabel = true;
                series3.MarkerStyle = MarkerStyle.Diamond;
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3.ToolTip = "入库金额: #VAL{c} 万元";

                series3.Label = "#VAL{F}";
                series3.Color = Color.FromArgb(143, 195, 31);

                Series series4 = new Series("销售金额");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series4.IsValueShownAsLabel = true;
                series4.MarkerStyle = MarkerStyle.Diamond;
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4.ToolTip = "销售金额: #VAL{c} 万元";

                series4.Label = "#VAL{F}";
                series4.Color = Color.FromArgb(137, 201, 151);

                Series series5 = new Series("退货金额");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series5.IsValueShownAsLabel = true;
                series5.MarkerStyle = MarkerStyle.Diamond;
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;
                series5.ToolTip = "退货金额: #VAL{c} 万元";

                series5.Label = "#VAL{F}";
                series5.Color = Color.FromArgb(88, 219, 213);


                #endregion

                #region 右图

                ////***************** 日常采购  ****************/
                //Series series5 = new Series("日常采购");
                //series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), Drpmode.SelectedValue, true);
                //series5.IsValueShownAsLabel = true;
                //series5.MarkerStyle = MarkerStyle.Diamond;
                //series5.BorderWidth = 1;
                //series5.ShadowOffset = 1;


                //series5.Label = "#VAL{F}";
                //series5.Color = Color.FromArgb(74, 172, 197);

                ////***************** 临时采购  ****************/
                //Series series6 = new Series("临时采购");
                //series6.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), Drpmode.SelectedValue, true);
                //series6.IsValueShownAsLabel = true;
                //series6.MarkerStyle = MarkerStyle.Diamond;
                //series6.BorderWidth = 1;
                //series6.ShadowOffset = 1;


                //series6.Label = "#VAL{F}";
                //series6.Color = Color.FromArgb(129, 101, 162);

                ////***************** 应急采购  ****************/
                //Series series7 = new Series("应急采购");
                //series7.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), Drpmode.SelectedValue, true);
                //series7.IsValueShownAsLabel = true;
                //series7.MarkerStyle = MarkerStyle.Diamond;
                //series7.BorderWidth = 1;
                //series7.ShadowOffset = 1;


                //series7.Label = "#VAL{F}";
                //series7.Color = Color.FromArgb(192, 80, 78);
                #endregion

                #region 绑定值，分3块日常采购，临时采购，应急采购（数量，采购金额）


                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {

                    // serise1，series5 日常采购

                    // serise1，series5 日常采购


                    //金额
                    series1.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());

                    series2.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["DistributeAmountTotal"].ToString()) / 10000).ToString());

                    series3.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());

                    series4.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["sellAmountTotal"].ToString()) / 10000).ToString());

                    series5.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["ReturnAmountTotal"].ToString()) / 10000).ToString());

                    ////配送率
                    //series5.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), (double.Parse(dt.Rows[i]["DistributeRate"].ToString())).ToString());

                    monch = dt.Rows[i]["DayTime"].ToString();

                    //series2.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), 0);
                    //series2.Points[s1].ToolTip = "0";

                    //series6.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), 0);
                    //series6.Points[s1].ToolTip = "0";

                    //series3.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), 0);
                    //series3.Points[s2].ToolTip = "0";

                    //series7.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(dt.Rows[i]["DayTime"].ToString()), 0);
                    //series7.Points[s2].ToolTip = "0";

                    // serise2，series6 临时采购


                    //s0++;
                    //s1++;
                    //s2++;



                }




                #endregion

                #region 数据绑定到图
                //清空Serise 避免确定按钮和下拉框同时绑定图表
                ChartPurchasing.Series.Clear();

                ChartInStorage.Series.Clear();


                ChartPurchasing.Series.Add(series1);

                //ChartInStorage.Series.Add(series5);

                ChartPurchasing.Series.Add(series2);

                //ChartInStorage.Series.Add(series6);

                ChartPurchasing.Series.Add(series3);
                ChartPurchasing.Series.Add(series4);
                ChartPurchasing.Series.Add(series5);
                //ChartInStorage.Series.Add(series7);
                #endregion

                #region 两张图片属性

                //ChartAreaPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].Area3DStyle.Enable3D = true;

                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisX.IsMarginVisible = true;
                //ChartInStorage.ChartAreas["ChartHosStorage"].Area3DStyle.Enable3D = false;


                //ChartAreaPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.MajorGrid.Interval = 1;
                //ChartAreaPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.LineWidth = 1;
                //ChartAreaPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.LineWidth = 1;

                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisX.MajorGrid.Interval = 1;
                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisX.LineWidth = 1;
                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.Title = "日期";

                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisY.Title = "配送率";
                //ChartInStorage.ChartAreas["ChartHosStorage"].AxisX.Title = "日期";
                #endregion

                #region 分组
                ChartPurchasing.Series["采购金额"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["配送金额"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["入库金额"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["销售金额"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["退货金额"]["StackedGroupName"] = "Group1";

                ChartPurchasing.Series["采购金额"]["DrawingStyle"] = "Cylinder";
                ChartPurchasing.Series["配送金额"]["DrawingStyle"] = "Cylinder";
                ChartPurchasing.Series["入库金额"]["DrawingStyle"] = "Cylinder";
                ChartPurchasing.Series["销售金额"]["DrawingStyle"] = "Cylinder";
                ChartPurchasing.Series["退货金额"]["DrawingStyle"] = "Cylinder";

                ChartPurchasing.Series["采购金额"]["PointWidth"] = "0.6";
                ChartPurchasing.Series["配送金额"]["PointWidth"] = "0.6";
                ChartPurchasing.Series["入库金额"]["PointWidth"] = "0.6";
                ChartPurchasing.Series["销售金额"]["PointWidth"] = "0.6";
                ChartPurchasing.Series["退货金额"]["PointWidth"] = "0.6";


                //ChartInStorage.Series["日常采购"]["StackedGroupName"] = "Group2";
                //ChartInStorage.Series["临时采购"]["StackedGroupName"] = "Group2";
                //ChartInStorage.Series["应急采购"]["StackedGroupName"] = "Group2";

                //ChartInStorage.ResetAutoValues();
                ChartPurchasing.ResetAutoValues();
                #endregion
				this.divshow1.Visible = true;
			}
			else
			{

				//this.tbChange.Visible = false;

				this.btnExcel.Visible = false;

				this.divshow1.Visible = false;

				//lblnull.Visible = true;

				this.ChartInStorage.Visible = false;

                this.ChartPurchasing.Visible = false;

				this.divshow1.Visible = false;

                //this.divTable.Visible = false;
			}

		}



		/// <summary>
		/// 绑定图表数据，第一次绑定时的初始化
		/// 董辉
		/// 20111-4-1 15:11
		/// </summary>
		/// <param name="row">当前行，也就是第一行</param>
		/// <param name="ParameType">类型 0,1,2</param>
		/// <param name="BaseI">日常采购循环因子</param>
		/// <param name="ComplementI">临时采购循环因子</param>
		/// <param name="AstrictI">应急采购循环因子</param>
        /// <param name="BaseCatalog">日常采购数量 0</param>
        /// <param name="Complementarity">临时采购数量 1</param>
        /// <param name="Astrictbreed">应急采购数量 2</param>
        /// <param name="BaseCatalogMoney">日常采购金额 0</param>
        /// <param name="ComplementarityMoney">临时采购金额 1</param>
        /// <param name="AstrictbreedMoney">应急采购金额2</param>
        private void BindChartInit(DataRow row, string ParameType, int BaseI, int ComplementI, int AstrictI, Series BaseCatalog, Series Complementarity, Series Astrictbreed, Series BaseCatalogMoney, Series ComplementarityMoney, Series AstrictbreedMoney)
		{
			if (row != null)
			{
				switch (ParameType)
				{
					case "0": //日常采购

                        //日常采购入库数量
						BaseCatalog.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()),
							double.Parse(row["DistributeRate"].ToString()));

                        //日常采购采购金额
						BaseCatalogMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), (Convert.ToDecimal(row["PurchaseAmountTotal"]) / 10000));

						//临时采购入库数量
                        Complementarity.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), 0);


                        //临时采购入库金额
                        ComplementarityMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), 0);


                        //应急采购入库数量
                        Astrictbreed.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), 0);


                        //应急采购采购金额
                        AstrictbreedMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), 0);
						

						break;

                   
				}
			}
		}

		/// <summary>
		/// 最终显示图表数据
		/// 董辉
		/// 2011-4-1 14:31
		/// </summary>
		/// <param name="row">当前行</param>
		/// <param name="ParameType">属于哪种类型,0,1,5</param>
		/// <param name="BaseI">目录索引</param>
		/// <param name="ComplementI">临时采购索引</param>
		/// <param name="AstrictI">应急采购索引</param>
		/// <param name="BaseCatalog">日常采购数量</param>
		/// <param name="Complementarity">临时采购数量</param>
		/// <param name="Astrictbreed">应急采购数量</param>
		/// <param name="BaseCatalogMoney">日常采购金额</param>
		/// <param name="ComplementarityMoney">临时采购金额</param>
		/// <param name="AstrictbreedMoney">应急采购金额</param>
        private void BindChartFinishData(DataRow row, string ParameType, int BaseI, int ComplementI, int AstrictI, Series BaseCatalog, Series Complementarity, Series Astrictbreed, Series BaseCatalogMoney, Series ComplementarityMoney, Series AstrictbreedMoney)
		{
			if (row != null)
			{
				switch (row["SortSorce"].ToString())
				{
					case "0": //日常采购

						BaseCatalog.Points.RemoveAt(BaseI - 1);
						//日常采购入库数量
						BaseCatalog.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), double.Parse(row["DistributeRate"].ToString()));

						BaseCatalogMoney.Points.RemoveAt(BaseI - 1);
						//日常采购采购金额
						BaseCatalogMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), (Convert.ToDecimal(row["PurchaseAmountTotal"].ToString()) / 10000));

						break;

                    case "1": //临时采购

                        Complementarity.Points.RemoveAt(ComplementI - 1);
                        //临时采购入库数量
                        Complementarity.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), double.Parse(row["DistributeRate"].ToString()));

                        ComplementarityMoney.Points.RemoveAt(ComplementI - 1);
                        //临时采购采购金额
                        ComplementarityMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), (Convert.ToDecimal(row["PurchaseAmountTotal"].ToString()) / 10000));

                        break;

                    case "2": //应急采购

                        Astrictbreed.Points.RemoveAt(AstrictI - 1);
                        //应急采购入库数量
                        Astrictbreed.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), double.Parse(row["DistributeRate"].ToString()));

                        AstrictbreedMoney.Points.RemoveAt(AstrictI - 1);
                        //应急采购采购金额
                        AstrictbreedMoney.Points.AddXY(HZ.Supervise.Web.Function.FormartDate(row["DayTime"].ToString()), (Convert.ToDecimal(row["PurchaseAmountTotal"].ToString()) / 10000));

                        break;
				}
			}
		}


		protected void btnExcel_Click(object sender, EventArgs e)
		{
			SuperviseQuery sq;
			if (ViewState["query"] != null)
			{
				sq = (SuperviseQuery)ViewState["query"];
			}
			else
			{
				sq = new SuperviseQuery();
				HZ.Supervise.Model.User user = CurrentAccount.GetUser();
				if (user.Org.Area.AreaID.Equals(Constants.CURRENT_DEPLOY_AREAID.ToString()))
				{ }
				else
				{
					sq.AreaID = user.Org.Area.AreaID;
				}
			}

			var model = new HZ.Data.QueryModel();
			model["AreaId"] = sq.AreaID;
			model["SortFields"] = "DayTime";
			string month = "";
			if (DateTime.Now.AddMonths(-12).Month <= 9) month = "0" + DateTime.Now.AddMonths(-12).Month.ToString();
			else month = DateTime.Now.AddMonths(-12).Month.ToString();
			string timeFrom = DateTime.Now.AddMonths(-12).Year + "" + month;
			if (string.IsNullOrEmpty(sq.Append))
			{
				model["append"] = " DayTime > '" + timeFrom + "'";
			}
			else
			{
				model["append"] = " DayTime > '" + timeFrom + "' and " + sq.Append;
			}

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
			System.Data.DataTable dt2 = iNew.DayTime_SortSorec_AreaId_GroupBy(model, 1, 0, out t);

			DataTable dt3 = dt2.Copy();

			DataView dv = dt3.DefaultView;

			dv.Sort = "DayTime desc";

			dt3 = dv.ToTable();

            //HZ.Supervise.Web.Function.AddColumn(dt3, "WareHouseRate", "SortSorce");
            //HZ.Supervise.Web.Function.ChangeRate_PS(dt3, "DistributeRate");

			HZ.Excel.ExcelHelper.ExportDataForWeb(dt3, this.gvwProcureCatalog);
		}

		protected void lbtnSee_Click(object sender, EventArgs e)
		{

		}

		protected void gvwProcureCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
		{

		}

		protected void gvwProcureCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "DirectByDetail")
			{

				string sessionid = Guid.NewGuid().ToString();
				SuperviseQuery query;
				if (ViewState["query"] == null)
				{
					query = new SuperviseQuery();
					query.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;
				}
				else
				{
					query = (SuperviseQuery)ViewState["query"];
				}
				try
				{
					query.Mid = e.CommandArgument.ToString();
				}
				catch { }
				Session[sessionid] = query;
				string url = "";
                if (query.AreaID.Substring(4, 2) == "00")//县区以上
                {
                    if (string.IsNullOrEmpty(Request.QueryString["aid"]))
                    {
                        url = "AreaofMonth.aspx";
                    }
                    else   //按医院查询
                    {
                        url = "AreaCurrentGoodsList.aspx";
                    }
                   
                }
                else
                {
                    url = "AreaCurrentGoodsList.aspx";
                }
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
				
			}
		}

		protected void drpmode_SelectedIndexChanged(object sender, EventArgs e)
		{
			Databind();
			BindChart();

		}

		/// <summary>
		/// 表头合并
		/// by张雄  2011.5.8
		/// </summary>
		/// <param name="sender">事件的发送源</param>
		/// <param name="e">参数</param>
		protected void gvwProcureCatalog_RowCreated(object sender, GridViewRowEventArgs e)
		{
			#region
			switch (e.Row.RowType)
			{
				case DataControlRowType.Header:
					TableCellCollection tcHeader = e.Row.Cells;
					tcHeader.Clear();
					tcHeader.Add(new TableHeaderCell());
					tcHeader[0].Attributes.Add("colspan", "6"); //跨Column
					SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
					if (sq != null)
					{
						tcHeader[0].Text = (sq.AreaName == null ? "地区汇总统计" : sq.AreaName) + "</th></tr><tr>";
					}
					else
					{
						tcHeader[0].Text = "地区汇总统计" + "</th></tr><tr>";
					}

					//第2行表头
                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "日期";

                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    //tcHeader[2].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "配送总金额";

                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    //tcHeader[5].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "销售总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "退货总金额";

					break;
			}
			#endregion
		}
	}
}
